package com.astar.controller.sort;


import com.astar.entity.PractiseCourseType;
import com.astar.service.IPractiseCourseTypeService;
import com.astar.service.impl.PractiseCourseTypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分类练习-科目类别表  前端控制器
 * 科目管理
 * </p>
 *
 * @author jobob
 * @since 2020-01-31
 */
@Controller
@RequestMapping("/courseManage")
public class PractiseCourseTypeController {

    @Autowired
    private IPractiseCourseTypeService practiseCourseTypeServiceImpl;

    @RequestMapping("/index")
    public Object toIndex(){
        return "courseManage/index";
    }


    @RequestMapping("/selectList")
    @ResponseBody
    public Object selectList(String name){
        List<PractiseCourseType> practiseCourseTypes = practiseCourseTypeServiceImpl.selectAll(name);
        return practiseCourseTypes;
    }

    @RequestMapping("/view")
    public Object view(Integer id, Model model){
        List imgsList = new ArrayList();
        PractiseCourseType practiseCourseType = practiseCourseTypeServiceImpl.selectForId(id);
        String[] s1=practiseCourseType.getImageUrl().split(",");
        for (int i =0;i<s1.length;i++){
            imgsList.add(s1[i]);
        }
        model.addAttribute("data",practiseCourseType);
        model.addAttribute("imgsList",imgsList);
        return "courseManage/view";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object deleteForId(PractiseCourseType practiseCourseType){
        Map<String,Object> map = new HashMap<>();

        int g = practiseCourseTypeServiceImpl.deleteForId(practiseCourseType);
        map.put("returnCode","0000");
        map.put("returnData",g);
        return map;
    }

    @RequestMapping("/deletes")
    @ResponseBody
    public Object deleteForIds(String arr){
        Map<String,Object> map = new HashMap<>();
        String[] array = arr.split(",");
        for (int i =0;i<array.length;i++){
            int id = Integer.parseInt(array[i]);
            PractiseCourseType practiseCourseType = new PractiseCourseType();
            practiseCourseType.setId(id);
            practiseCourseTypeServiceImpl.deleteForIds(practiseCourseType);
        }
        map.put("returnCode","0000");
        return map;
    }

    @RequestMapping("/toAdd")
    public Object toAdd(){
        return "courseManage/add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(PractiseCourseType practiseCourseType){
        Map<String,Object> map = new HashMap<>();
        int i = practiseCourseTypeServiceImpl.add(practiseCourseType);
        map.put("returnCode","0000");
        return map;
    }

    @RequestMapping("/toUpdate")
    public Object toUpdate(Integer id,Model model){
        PractiseCourseType practiseCourseType = practiseCourseTypeServiceImpl.selectForId(id);
        model.addAttribute("data",practiseCourseType);
        return "courseManage/update";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(PractiseCourseType practiseCourseType){
        Map<String,Object> map = new HashMap<>();
        int i = practiseCourseTypeServiceImpl.updateForId(practiseCourseType);
        map.put("returnCode","0000");
        return map;
    }



}
